<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="订单编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatusArr">
        <el-select v-model="queryParams.orderStatusArr" placeholder="全部" multiple clearable>
          <el-option
            v-for="dict in dict.type.order_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="全部" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="productTitles">
        <el-input
          v-model="queryParams.productTitles"
          placeholder="商品名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker
          v-model="queryParams.orderTimeArr"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item class="search">
        <hide-search :show-search.sync="showSearch"></hide-search>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="table-content">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            icon="el-icon-upload2"
            size="mini"
            @click="handleExport"
            v-hasPermi="['project:order:export']"
          >导出
          </el-button>
        </el-col>
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>

      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="编号" width="140" align="center" prop="id"/>
        <el-table-column label="订单编号" width="140" align="center" prop="orderNo"/>
        <el-table-column label="订单类型" align="center" prop="orderType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" width="240" prop="productTitles" show-overflow-tooltip/>
        <el-table-column label="支付方式" align="center" prop="payType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.pay_type" :value="scope.row.payType"/>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" width="160" align="center" prop="orderTime"/>
        <el-table-column label="运费" align="center" prop="freightPrice"/>
        <el-table-column label="订单金额" align="center" prop="realPrice"/>
        <el-table-column label="订单状态" align="center" prop="orderStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_state" :value="scope.row.orderStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="物流公司" width="120" align="center" prop="logisticsFirm">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.logistics_firm" :value="scope.row.logisticsFirm"/>
          </template>
        </el-table-column>
        <el-table-column label="物流编号" align="center" prop="logisticsNo" width="180"/>
        <el-table-column label="收货信息" align="center" width="240">
          <template slot-scope="scope">
            <div v-if="scope.row.userAddress" class="embedded-table">
              <el-table :data="[scope.row.userAddress]" size="mini" :show-header="false" :border="false"
                        class="no-border-table"
              >
                <el-table-column>
                  <template slot-scope="innerScope">
                    <el-row>
                      <el-col :span="6"><strong>收货人：</strong></el-col>
                      <el-col :span="18">{{ innerScope.row.name || "--" }}</el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="6"><strong>电话：</strong></el-col>
                      <el-col :span="18">{{ innerScope.row.phone || "--" }}</el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="6"><strong>地址：</strong></el-col>
                      <el-col :span="18">
                        {{ innerScope.row.address + (innerScope.row.room ? " " + innerScope.row.room : "") || "--" }}
                      </el-col>
                    </el-row>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="退款金额" align="center" prop="backPrice" show-overflow-tooltip/>
        <el-table-column label="退货物流公司" width="120" align="center" prop="backLogisticsFirm" show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.logistics_firm" :value="scope.row.backLogisticsFirm"/>
          </template>
        </el-table-column>
        <el-table-column label="退货物流编号" width="100" align="center" prop="backLogisticsNo" show-overflow-tooltip/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              :disabled="scope.row.orderStatus !== '2'"
              @click="handleUpdate(scope.row, 'send')"
              v-hasPermi="['project:order:edit']"
            >发货
            </el-button>
            <el-button
              size="mini"
              icon="el-icon-edit"
              :disabled="scope.row.orderStatus !== '5'"
              @click="handleDelete(scope.row)"
              v-hasPermi="['project:order:remove']"
            >退款
            </el-button>
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row, 'edit')"
              v-hasPermi="['project:order:edit']"
            >修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 修改、发货 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <div v-if="openStatus === 'send'">
          <el-form-item label="物流公司" prop="logisticsFirm">
            <el-select v-model="form.logisticsFirm" filterable clearable placeholder="请选择物流公司" style="width: 100%">
              <el-option
                v-for="dict in dict.type.logistics_firm"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="物流编号" prop="logisticsNo">
            <el-input
              clearable
              v-model="form.logisticsNo"
              placeholder="请输入物流编号"
              maxlength="30"
              style="width:100%"
            />
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="商品名称" prop="productTitles">
            <el-input
              clearable
              style="width:100%"
              v-model="form.productTitles"
              type="textarea"
              placeholder="请输入内容"
              maxlength="3000"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="订单金额" prop="realPrice">
            <el-input
              clearable
              v-model="form.realPrice"
              @keyup.native="form.realPrice = form.realPrice.replace(/^\.+|[^\d.]/g,'')"
              placeholder="请输入订单金额"
              maxlength="10"
              style="width:100%"
            />
          </el-form-item>
          <el-form-item label="退款金额" prop="backPrice">
            <el-input
              clearable
              v-model="form.backPrice"
              @keyup.native="form.backPrice = form.backPrice.replace(/^\.+|[^\d.]/g,'')"
              placeholder="请输入退款金额"
              maxlength="10"
              style="width:100%"
            />
          </el-form-item>
          <el-form-item label="买家备注" prop="remark">
            <el-input
              clearable
              style="width:100%"
              v-model="form.remark"
              type="textarea"
              placeholder="请输入内容"
              maxlength="500"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="物流公司" prop="logisticsFirm">
            <el-select v-model="form.logisticsFirm" filterable clearable placeholder="请选择物流公司" style="width: 100%">
              <el-option
                v-for="dict in dict.type.logistics_firm"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="物流编号" prop="logisticsNo">
            <el-input
              clearable
              v-model="form.logisticsNo"
              placeholder="请输入物流编号"
              maxlength="30"
              style="width:100%"
            />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 退款对话框 -->
    <el-dialog :title="title" :visible.sync="refundOpen" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="refundForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="退款金额" prop="backPrice">
          <el-input
            clearable
            v-model="form.backPrice"
            @keyup.native="form.backPrice = form.backPrice.replace(/^\.+|[^\d.]/g,'')"
            placeholder="请输入退款金额"
            maxlength="10"
            style="width:100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitRefundForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, getOrder, addOrder, updateOrder, delOrder, refund} from "@/api/order";
import Template from "@/views/system/template";

export default {
  name: "Config",
  components: {Template},
  dicts: ["sys_yes_no", "order_state", "order_type", "pay_type", "logistics_firm"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 参数表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      refundOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: undefined,
        orderType: undefined,
        productTitles: undefined,
        orderStatusArr: [],
        orderTimeArr: []
      },
      openStatus: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        configName: [
          {required: true, message: "参数名称不能为空", trigger: "blur"}
        ],
        configKey: [
          {required: true, message: "参数键名不能为空", trigger: "blur"}
        ],
        configValue: [
          {required: true, message: "参数键值不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.refundOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderType: undefined,
        userAddressId: undefined,
        productId: undefined,
        productTitles: undefined,
        productNum: undefined,
        userId: undefined,
        payType: undefined,
        undefined: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.orderStatusArr = [];
      this.queryParams.orderTimeArr = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 发货按钮操作 */
    handleUpdate(row, type) {
      this.loading = true;
      this.openStatus = type;
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.form.orderStatus = "3";
        this.open = true;
        this.title = type === "发货" ? "发货" : "修改订单";
      });
    },
    /** 退款按钮操作 */
    handleDelete(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.form.backPrice = this.form.realPrice;
        this.refundOpen = true;
        this.title = "退款";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 退款 */
    submitRefundForm: function() {
      this.$refs["refundForm"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            refund(this.form).then(response => {
              this.$modal.msgSuccess("退款成功");
              this.refundOpen = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download("/project/order/export", {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    }
  }
};
</script>
<style>
.no-border-table {
  border: none !important;
}

.no-border-table .el-table {
  background-color: transparent !important;
  border: none !important;
}

.no-border-table .el-table__wrapper {
  border: none !important;
}

.no-border-table .el-table th,
.no-border-table .el-table tr,
.no-border-table .el-table td {
  background-color: transparent !important;
  border-bottom: none !important;
  border-right: none !important;
}

.no-border-table .el-table__empty-block {
  border: none !important;
}

.embedded-table {
  padding: 0;
}
</style>
